package web;

import Utils.web.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String token = WebUtils.getKaptchaToken(request);
        String code = request.getParameter("code");
        String username = request.getParameter("username");
        if (token != null && token.equalsIgnoreCase(code)) {
            System.out.println("注册成功：" + username);
            response.sendRedirect(request.getContextPath() + "/ok.jsp");
        } else {
            System.out.println("验证码错误！");
            request.setAttribute("username", username);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        request.getRequestDispatcher("/ok.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
